package com.jingdianjichi.club.oss.adapter;

import com.jingdianjichi.club.oss.enty.FileInfo;
import com.jingdianjichi.club.oss.util.MinioUtil;
import lombok.SneakyThrows;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * minio服务
 * @author 86156
 * @version 1.0
 * @date 2024-10-25 12:58
 */
public class MinioStorageAdapter implements StorageAdapter {
    @Resource
    private MinioUtil minioUtil;

    @Override
    @SneakyThrows
    public void createBucket(String bucketName) {
        minioUtil.createBucket(bucketName);
    }

    /**
     * 上传文件
     * @param file 文件
     * @param bucketName 桶名称
     * @param objectName 对象名称
     */
    @Override
    @SneakyThrows
    public void uploadFile(MultipartFile file, String bucketName, String objectName) {
        minioUtil.createBucket(bucketName);
        if(objectName != null){
            minioUtil.uploadFile(file.getInputStream(),bucketName,objectName+"/"+file.getName());
        }else{
            minioUtil.uploadFile(file.getInputStream(),bucketName,file.getName());
        }
    }

    @Override
    @SneakyThrows
    public List<String> listBucket() {
        return minioUtil.listBucket();
    }

    @Override
    @SneakyThrows
    public List<FileInfo> getAllFile(String bucketName) {
        return minioUtil.getAllFile(bucketName);
    }

    @Override
    @SneakyThrows
    public InputStream downLoadFile(String bucketName, String objectName) {
        return minioUtil.downLoadFile(bucketName,objectName);
    }

    @Override
    @SneakyThrows
    public void deleteBucket(String bucketName) {
        minioUtil.deleteBucket(bucketName);
    }

    @Override
    @SneakyThrows
    public void deleteFile(String bucketName, String objectName) {
        minioUtil.deleteFile(bucketName,objectName);
    }

    @Override
    @SneakyThrows
    public String getUrl(String bucketName, String objectName) {
        return minioUtil.getObjectUrl(bucketName,objectName);
    }

}
